//
//  StoryHeaderView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/31.
//

#import "StoryHeaderView.h"
#import "CommonUtil.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import <SDWebImage/SDWebImage.h>
#import "ChangeView.h"
#import "StoryInfoView.h"
@interface StoryHeaderView()
@property (strong, nonatomic) UIView * line1;
@property (strong, nonatomic) UIWebView * webView;
@property (strong, nonatomic) UILabel * updateLabel;
@property (strong, nonatomic) UIImageView * adImageView;
@property (strong, nonatomic) UIView * shareView;
@property (strong, nonatomic) SDCycleScrollView * cycleScrollView;
@property (strong, nonatomic) UIImageView * noticeImageView;
@property (strong, nonatomic) UILabel * noticeLabel;
@property (strong, nonatomic) UIView * line2;
@property (strong, nonatomic) UIView * line3;
@property (strong, nonatomic) UIButton * eyeBtn;

@property (strong, nonatomic) UIView * indexView;
@property (strong, nonatomic) ChangeView * changeView;

@property (strong, nonatomic) StoryInfoView * infoView;

@end

@implementation StoryHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

        
    }
    return self;
}

-(void)setType:(DetailPageType)type{
    _type = type;
    
    if (self.type == DetailPageTypeBigStory) {
        CGFloat w = self.width/4;
        for (NSInteger i = 0; i < 4; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(i * w, 0 , w, AD(50));
            btn.titleLabel.numberOfLines = 0;
            btn.titleLabel.font = [UIFont systemFontOfSize:AD(13)];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            [btn setTitleColor:RGB(54.0, 54.0, 54.0) forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(followBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            if (i == 0) {
                [btn setTitle:@"2390\n获赞" forState:UIControlStateNormal];
            }else if (i == 1){
                [btn setTitle:@"2390\n粉丝" forState:UIControlStateNormal];
            }else if (i == 2){
                [btn setTitle:@"2390\n关注" forState:UIControlStateNormal];
            }else if (i == 3){
                [btn setTitle:@"2390\n成长值" forState:UIControlStateNormal];
            }
        }
        [self addSubview:self.line1];
        self.eyeBtn.hidden = YES;
    }else if(self.type == DetailPageTypeGallery) {
        self.line1.hidden = YES;
        self.line1.frame = CGRectZero;
        self.updateLabel.top = 0;
        self.eyeBtn.top = 0;
        self.webView.top = self.updateLabel.bottom;
        self.adImageView.top = self.webView.bottom;
        self.shareView.top = self.adImageView.bottom;
        self.cycleScrollView.top = self.shareView.bottom + AD(10);
        self.noticeImageView.top = self.cycleScrollView.bottom + AD(20);
        self.noticeLabel.top = self.noticeImageView.top;
        self.updateLabel.text = @"2021-05-08 10:20";
    }else if(self.type == DetailPageTypeDog || self.type == DetailPageTypeHumor || self.type == DetailPageTypeNorFowl) {
        self.eyeBtn.hidden = YES;
        [self addSubview:self.infoView];
        [self addSubview:self.line1];
    }
    
    
    [self addSubview:self.webView];
    [self addSubview:self.adImageView];
    [self addSubview:self.updateLabel];
    [self addSubview:self.eyeBtn];
    [self addSubview:self.shareView];
    [self addSubview:self.cycleScrollView];
    [self addSubview:self.noticeImageView];
    [self addSubview:self.noticeLabel];
    [self addSubview:self.line2];
    [self addSubview:self.hotBtn];
    [self addSubview:self.onlyBtn];
    [self addSubview:self.line3];
    [self addSubview:self.indexView];
    [self addSubview:self.changeView];
    [self changeBtnClick:self.hotBtn];
}



- (void)followBtnClick:(UIButton *)sender{
    NSLog(@"%@",sender.titleLabel.text);
}

- (void)timerClick{
   
    self.noticeImageView.hidden = !self.noticeImageView.hidden;
    
}

- (void)changeBtnClick:(UIButton *)sender{
    self.selectBtn.selected = false;
    self.selectBtn = sender;
    self.selectBtn.selected = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.indexView.centerX = sender.centerX;
    }];
}

#pragma mark - lazy
-(UIView *)line1{
    if (!_line1) {
        _line1 = [[UIView alloc] initWithFrame:CGRectMake(0, AD(70), SCREEN_WIDTH, AD(1))];
        _line1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    }
    return _line1;
}
-(UIView *)line2{
    if (!_line2) {
        _line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.noticeLabel.bottom + AD(30), SCREEN_WIDTH, AD(10))];
        _line2.backgroundColor = RGB(241.0, 241.0, 241.0);
    }
    return _line2;
}
-(UIView *)line3{
    if (!_line3) {
        _line3 = [[UIView alloc] initWithFrame:CGRectMake(0, _line2.bottom + AD(50), SCREEN_WIDTH, AD(1))];
        _line3.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    }
    return _line3;
}

-(UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.line1.bottom, SCREEN_WIDTH, AD(250))];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
    }
    return _webView;
}

-(UIImageView *)adImageView{
    if (!_adImageView) {
        _adImageView = [[UIImageView alloc] initWithFrame:CGRectMake(AD(15), self.webView.bottom + AD(10), SCREEN_WIDTH-AD(30), AD(50))];
        [_adImageView sd_setImageWithURL:[NSURL URLWithString:@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01a260572c31aa32f875a399b773f0.png%401280w_1l_2o_100sh.png&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625036679&t=58dd3184e21ef72919f1e52d48a58e4f"]];
    }
    return _adImageView;
}

-(UILabel *)updateLabel{
    if (!_updateLabel) {
        _updateLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(15),self.adImageView.bottom + AD(15) , SCREEN_WIDTH-AD(15), AD(30))];
        _updateLabel.textColor = [UIColor lightGrayColor];
        _updateLabel.font = [UIFont systemFontOfSize:AD(13)];
        _updateLabel.text = @"最后更新时间:2021-02-02 20:20:12";
    }
    return _updateLabel;
}

-(UIView *)shareView{
    if (!_shareView) {
        _shareView = [[UIView alloc] initWithFrame:CGRectMake(0, self.updateLabel.bottom, SCREEN_WIDTH, AD(80))];
        _shareView.backgroundColor = RGB(255.0, 247.0, 245.0);
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(AD(15), AD(20), AD(250), AD(20))];
        label.text = @"分享是一种快乐也是一种美德!";
        label.textColor = RGB(255.0, 107.0, 130.0);
        [_shareView addSubview:label];
        
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareView addSubview:shareBtn];
        [shareBtn setTitle:@"分享有礼" forState:UIControlStateNormal];
        [shareBtn setTitleColor:label.textColor forState:UIControlStateNormal];
        shareBtn.titleLabel.font = [UIFont systemFontOfSize:AD(17)];
        shareBtn.frame = CGRectMake(SCREEN_WIDTH-AD(100), 0, AD(80), AD(30));
        shareBtn.layer.cornerRadius = AD(15);
        shareBtn.clipsToBounds = YES;
        shareBtn.layer.borderColor = label.textColor.CGColor;
        shareBtn.layer.borderWidth = AD(1);
        shareBtn.centerY = label.centerY;
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(AD(15), shareBtn.bottom + AD(8), AD(150), AD(25))];
        label2.textColor = [UIColor blackColor];
        label2.textAlignment = NSTextAlignmentRight;
        label2.right = SCREEN_WIDTH-AD(20);
        label2.text = @"已有203人获得奖励";
        [self setAttributeWithLabel:label2 redText:@"203"];
        [_shareView addSubview:label2];
    }
    return _shareView;
}


- (void)setAttributeWithLabel:(UILabel *)label redText:(NSString *)redText{
    NSMutableAttributedString *attribut = [[NSMutableAttributedString alloc]initWithString:label.text];
    NSRange range1 = [label.text rangeOfString:redText];
    [attribut addAttribute:NSForegroundColorAttributeName value:RGB(255.0, 107.0, 130.0) range:range1];
    label.attributedText = attribut;
}


-(SDCycleScrollView *)cycleScrollView{
    if (!_cycleScrollView) {
        NSArray *arr = @[@"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=4012287188,2486384544&fm=26&gp=0.jpg",@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Fback_pic%2F04%2F59%2F57%2F88586b45b921f7f.jpg&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625036679&t=d249ec42d51e6fd472354eb51939c173",@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpicnew12.photophoto.cn%2F20180621%2Fxiaoqingxinwangyexuanchuanlunbobanner-29888922_1.jpg&refer=http%3A%2F%2Fpicnew12.photophoto.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625036679&t=594ec660c77a503d418d64f01db9c4f6"];
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, self.shareView.bottom + AD(10), SCREEN_WIDTH, AD(50)) imageURLStringsGroup:arr];
    }
    return _cycleScrollView;
}

-(UIImageView *)noticeImageView{
    if (!_noticeImageView) {
        _noticeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(AD(15), self.cycleScrollView.bottom + AD(20), AD(20), AD(20))];
        _noticeImageView.image = [UIImage imageNamed:@"icon_laba"];
        _noticeImageView.hidden = YES;
    }
    return _noticeImageView;
}

-(UILabel *)noticeLabel{
    if (!_noticeLabel) {
        _noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(AD(50),_noticeImageView.top , SCREEN_WIDTH-AD(80), AD(35))];
        _noticeLabel.textColor = RGB(255.0, 107.0, 130.0);
        _noticeLabel.font = [UIFont systemFontOfSize:AD(13)];
        _noticeLabel.text = @"内容后台配置内容后台配置内容后台配置内容后台配置内容后台配置内容后台配置内容";
        _noticeLabel.numberOfLines = 0;
    }
    return _noticeLabel;
}

-(UIButton *)hotBtn{
    if (!_hotBtn) {
        _hotBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_hotBtn setTitle:@"热门评论" forState:UIControlStateNormal];
        [_hotBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_hotBtn setTitleColor:RGB(255.0, 107.0, 130.0) forState:UIControlStateSelected];
        [_hotBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _hotBtn.titleLabel.font = [UIFont systemFontOfSize:AD(17)];
        _hotBtn.frame = CGRectMake(AD(10), self.line2.bottom, AD(80), AD(50));
    }
    return _hotBtn;
}

-(UIButton *)onlyBtn{
    if (!_onlyBtn) {
        _onlyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_onlyBtn setTitle:@"只看楼主" forState:UIControlStateNormal];
        [_onlyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_onlyBtn setTitleColor:RGB(255.0, 107.0, 130.0) forState:UIControlStateSelected];
        [_onlyBtn addTarget:self action:@selector(changeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _onlyBtn.titleLabel.font = [UIFont systemFontOfSize:AD(17)];
        _onlyBtn.frame = CGRectMake(_hotBtn.right + AD(50), self.line2.bottom, AD(80), AD(50));
    }
    return _onlyBtn;
}

-(UIView *)indexView{
    if (!_indexView) {
        _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, self.line3.top, self.hotBtn.width, AD(1))];
        _indexView.backgroundColor = RGB(255.0, 107.0, 130.0);
    }
    return _indexView;
}

-(ChangeView *)changeView{
    if (!_changeView) {
        _changeView = [[ChangeView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-AD(110), AD(10), AD(90), AD(30))];
        _changeView.centerY = self.hotBtn.centerY;
    }
    return _changeView;
}

-(UIButton *)eyeBtn{
    if (!_eyeBtn) {
        _eyeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_eyeBtn setImage:[UIImage imageNamed:@"icon_liuhe_eye"] forState:UIControlStateNormal];
        [_eyeBtn setTitle:@"1123" forState:UIControlStateNormal];
        [_eyeBtn setTitleColor:self.updateLabel.textColor forState:UIControlStateNormal];
        _eyeBtn.titleLabel.font = self.updateLabel.font;
        _eyeBtn.frame = CGRectMake(AD(130), 0, AD(50), self.updateLabel.height);
      //  _eyeBtn.backgroundColor = [UIColor redColor];
    }
    return _eyeBtn;
}

-(StoryInfoView *)infoView{
    if (!_infoView) {
        _infoView = [[StoryInfoView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(70))];
    }
    return _infoView;
}

@end
